#include<iostream>
#include<string.h>
using std::cout;
using std::cin;
using std::endl;

class Computer
{
public:
    //构造函数
    Computer(const char* brand,double price);
    //类外实现构造函数

    //拷贝构造
    Computer(const Computer & x);
    //类外实现,指针的情况存在深浅拷贝的问题

    //析构
    ~Computer();

    //赋值运算函数
    Computer & operator=(const Computer & x)
    {
        //避免由于两对象数组长度不一
        //先判断是否同一对象？
        //先释放
        //再拷贝
        //再返回
        if(this!=&x)
        {
            delete []_brand;
            _brand=new char[strlen(x._brand)+1]();
            strcpy(_brand,x._brand);
            _price=x._price;
        }
    return *this;
    }

    void print()
    {
        cout<<"("
            <<_brand
            <<","<<_price
            <<")"<<endl;
    }
private:
    //类成员
    char * _brand;
    double _price;
};
//类外实现构造
Computer::Computer(const char* brand,double price)
    :_brand(new char[strlen(brand)+1]())
     ,_price(price)
{
    strcpy(_brand,brand);
}

//类外实现析构

Computer::~Computer()
{
    if(_brand)
    {
        delete [] _brand;
        _brand=nullptr;
    }
    cout<<"~Computer done"<<endl;
}


//类外实现拷贝构造

Computer::Computer(const Computer& x)
    :_brand(new char[strlen(x._brand)+1]())
     ,_price(x._price)
{
    strcpy(_brand,x._brand);
}
Computer pc1("lenovo",4500);

/* void func(Computer &p) */
void func(Computer p)
{
    p.print();
}

void test()
{
    Computer pt("xiaomi",3000);
    func(pt);
}

int main()
{
    Computer p2=pc1;
    p2.print();
    test();

return 0;
}

